Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle empty course_overviews in get_courses_order_by function #35912

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Ali-Salman29
Copy link

Description

This pull request fixes an issue in the get_courses_order_by function where an empty course_overviews queryset caused an error. Previously, the function attempted to call order_by() on an empty or invalid queryset, leading to unexpected crashes. This change ensures that the function explicitly checks if course_overviews is empty and returns an empty list in such cases.

Changes Made

  • Added a condition to return an empty list if course_overviews is None or empty.
  • Improved the robustness of the function to handle edge cases gracefully.
Traceback (most recent call last):
  File "/openedx/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pyenv/versions/3.11.8/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/openedx/venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/venv/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/venv/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/venv/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/openedx/venv/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/openedx/venv/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/rest_api/v2/views/home.py", line 136, in get
    courses, in_process_course_actions = get_course_context_v2(request)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/utils.py", line 1666, in get_course_context_v2
    courses_iter, in_process_course_actions = get_courses_accessible_to_user(request, org)
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pyenv/versions/3.11.8/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/views/course.py", line 772, in get_courses_accessible_to_user
    courses, in_process_course_actions = _accessible_courses_list_from_groups(request)
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/views/course.py", line 580, in _accessible_courses_list_from_groups
    courses_list = get_filtered_and_ordered_courses(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/views/course.py", line 485, in get_filtered_and_ordered_courses
    course_overviews = get_courses_order_by(order, course_overviews)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/openedx/edx-platform/cms/djangoapps/contentstore/views/course.py", line 629, in get_courses_order_by
    return course_overviews.order_by(order_query)

image

Supporting information

This fixes the following traceback error:

return course_overviews.order_by(order_query)
AttributeError: 'NoneType' object has no attribute 'order_by'

The error occurred during course filtering and sorting operations in the CMS, where an invalid or empty course_overviews queryset caused the failure.

Testing instructions

  1. Navigate to the CMS interface where courses are listed.
  2. Simulate a scenario with no available courses or a None queryset for course_overviews.
  3. Verify that the page loads correctly without errors and displays an empty list of courses.
  4. Ensure that course sorting works as expected when valid courses are present.

Deadline

None

Other information

  • This change does not introduce migrations, security issues, or deprecations.
  • No configuration changes are required.

@Ali-Salman29 Ali-Salman29 force-pushed the alisalman/fix-studio-courses-list branch from 2193846 to 45fb387 Compare November 26, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant